package com.verisette.ncos.service.approver.bizunit;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;

public class CardApproveBU extends CommonBusinessUnit{

	private Logger LOG = Logger.getLogger(CardApproveBU.class);
	@Override
	public void execute(ProcessContext p) throws ProcessException 
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing CardApproveBU...");
		}
		UserBean user = p.getUserBean();
		
		String id = (String)p.getValueObject();
		
		TransCustomerOrder entity = (TransCustomerOrder) commonDAO.findById(
				TransCustomerOrder.class, Integer.parseInt(id));
		
		Date d = new Date();
		entity.setStatus(ProjectConstant.STATUS_APPROVE_Y);
		entity.setUpdatedDate(new Timestamp(d.getTime()));
		entity.setUpdatedBy(user.getUsername());
		MasterUserProfile masterUserProfile = (MasterUserProfile)user;
		entity.setUpdatedUspId(masterUserProfile.getUspId());
		
		if(entity.getApprovedUspId()==null){
			//Approved01
			entity.setApprovedDate(new Timestamp(d.getTime()));
			entity.setApprovedBy(user.getUsername());
			entity.setApprovedUspId(masterUserProfile.getUspId());
		} else {
			//Approved02
			entity.setStatus(ProjectConstant.STATUS_APPROVE02_Y);
			entity.setApproved02Date(new Timestamp(d.getTime()));
			entity.setApproved02By(user.getUsername());
			entity.setApproved02UspId(masterUserProfile.getUspId());
		}
		
		commonDAO.update(entity);
		
		p.setValueObject(entity);
	}

}
